package emp.servlet.user;

import emp.dao.UserDao;
import emp.entity.PageInfo;
import emp.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 叶枫
 * @date 2024/12/18.
 */

@WebServlet("/user")
public class AddServlet extends HttpServlet {
    private UserDao userDao;

    @Override
    public void init() throws ServletException {
        userDao = new UserDao();
    }
    public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        String action = request.getParameter("action");

        if ("list".equals(action)){
            list(request, response);
        }
        if ("getById".equals(action)){
            getById(request, response);
        }
        if ("delete".equals(action)){
            delete(request, response);
        }
    }
    public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        String action = request.getParameter("action");
        if ("add".equals(action)){
            add(request, response);
        }

        if ("update".equals(action)){
            update(request, response);
        }
    }
    public void list(HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        String pageNumber = request.getParameter("pageNumber");
        String pageSize = request.getParameter("pageSize");
        PageInfo<User> userPageInfo = userDao.listByPage(Integer.parseInt(pageNumber), Integer.parseInt(pageSize), null);
        request.setAttribute("userList", userPageInfo);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
    public void add(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(role);
        boolean success = userDao.add(user);
      response.sendRedirect("/user?action=list&pageNumber=1&pageSize=10");
    }
    public void delete(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        String userId = request.getParameter("id");
        boolean success = userDao.delete(Integer.parseInt(userId));
        response.sendRedirect("/user?action=list&pageNumber=1&pageSize=10");
    }
    public void update(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        String userId = request.getParameter("id");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        User user = new User();
        user.setUserId(Integer.parseInt(userId));
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(role);
        userDao.update(user);
        response.sendRedirect("/user?action=list&pageNumber=1&pageSize=10");
    }
    public void getById(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        String userId = request.getParameter("id");
        User user = userDao.getById(Integer.parseInt(userId));
        request.setAttribute("user", user);
        request.getRequestDispatcher("/addUser.jsp").forward(request, response);
    }

}
